import { cloudRequest } from './api/cloudRequest';
import ErrorHandler from './util/mox_onError/error';
import * as Utils from './util/util';
// import Aegis from 'aegis-mp-sdk';

App({
    onLaunch: function () {
        if (!wx.cloud) {
            console.error('请使用 2.2.3 或以上的基础库以使用云能力')
        } else {
            wx.cloud.init({
                // env: 'dev-0g2emurb59ee4031',
                traceUser: true,
            })
        }
        this.getSystemInfo();
        this.updateApp();
        this.getUserInfo().then(userInfo => {
            mox.init({
                uid: userInfo.openid
            });
            // this.aegis = new Aegis({
            //     id: 'yaoVL8AlJ1WPMDq4dG', // 上报 id
            //     uin: userInfo.openid, // 用户唯一 ID（可选）
            //     reportApiSpeed: true, // 接口测速
            //     spa: true // spa 应用页面跳转的时候开启 pv 计算
            // });
        });
    },
    onShow() {
        // 监听网络状态
        wx.onNetworkStatusChange((res) => {
            this.globalData.isNetworkConnected = res.isConnected;
        });
    },
    aegis: '',
    cloud: cloudRequest,
    globalData: {
        userInfo: {}, //用户信息
        isNetworkConnected: true, //网络状态是否连接
        systemInfo: {},
        titleHeight: 0,
    },
    stat(key) {
        mox.stat({
            key,
            uid: this.globalData.userInfo.openid || ''
        })
    },
    // 获取系统信息
    getSystemInfo() {
        wx.getSystemInfo({
            success: res => {
                this.globalData.systemInfo = res;
                let menuButtonData = wx.getMenuButtonBoundingClientRect();

                this.globalData.titleHeight =
                    menuButtonData.height +
                    (menuButtonData.top - this.globalData.systemInfo.statusBarHeight) * 2;

                this.globalData.titlePaddingRight =
                    menuButtonData.width +
                    (this.globalData.systemInfo.windowWidth - menuButtonData.right);
            },
            fail: () => {
                this.globalData.systemInfo = {
                    statusBarHeight: 20
                };
                this.globalData.titleHeight = 40;
            }
        });
    },
    getUserInfo() {
        return new Promise((resolve, reject) => {
            if (this.globalData.userInfo && this.globalData.userInfo.openid) {
                resolve(this.globalData.userInfo);
            } else {
                // this.cloud({
                //     name: 'spellUser',
                //     data: {
                //         $url: 'get_user_openid'
                //     }
                // }).then(res => {
                //     this.globalData.userInfo = res;
                //     resolve(res);
                // }).catch(err => {
                //     reject(err);
                // })
            }
        });
    },
    // 更新版本
    updateApp() {
        let that = this;
        const updateManager = wx.getUpdateManager();
        updateManager.onCheckForUpdate(function (res) {
            // 请求完新版本信息的回调
            // nothing
            false && res;
        });
        updateManager.onUpdateReady(function () {
            wx.showModal({
                title: '更新提示',
                showCancel: false,
                content: '新版本已经准备好，请重启应用',
                success: function (res) {
                    if (res.confirm) {
                        // 新的版本已经下载好，调用 applyUpdate 应用新版本并重启
                        updateManager.applyUpdate();
                    }
                },
            });
        });
        updateManager.onUpdateFailed(function () {
            // 新的版本下载失败
            wx.showModal({
                title: '更新提示',
                content: '新版本下载失败',
                showCancel: true,
                confirmText: '重试',
                success: function (res) {
                    if (res.confirm) {
                        // 新的版本已经下载好，调用 applyUpdate 应用新版本并重启
                        that.updateApp();
                    }
                },
            });
        });
    },
    onError(msg) {
        ErrorHandler.onError(msg);
    },
    onUnhandledRejection(evt) {
        ErrorHandler.onUnhandledRejection(evt);
    },
});
